﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using ByteSite.DBUtility;
using ByteSite.Common;
using SqlSugar;
using System.Linq.Expressions;

namespace ByteSite.DAL
{
    /// <summary>
    /// 数据访问类:站点支付方式表
    /// </summary>
    public partial class site_oauth
    {
        private string databaseprefix; //数据库表名前缀
        private SqlSugarClient db;
        public site_oauth(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
            db = new DBUtility.DbContext().Db;
        }

        /// <summary>
        /// 获取支付平台实体
        /// </summary>
        public List<Entitys.dt_site_oauth> GetList(int top, Expression<Func<Entitys.dt_site_oauth, bool>> whereExpression)
        {
            if (top == 0)
            {
                return db.Queryable<Entitys.dt_site_oauth, Entitys.dt_oauth_app>((s, a) => new JoinQueryInfos(JoinType.Inner, a.id == s.oauth_id))
                    .Select((s, a) => new Entitys.dt_site_oauth
                    {
                        id = s.id,
                        site_id = s.site_id,
                        oauth_id = s.oauth_id,
                        title = s.title,
                        app_id = s.app_id,
                        app_key = s.app_key,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        ptitle = a.title,
                        img_url = a.img_url,
                        remark = a.remark,
                        api_path = a.api_path,
                        is_lock = a.is_lock
                    })
                    .Where(s => s.is_lock == 0)
                    .Where(whereExpression)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .ToList();
            }
            else
            {
                return db.Queryable<Entitys.dt_site_oauth, Entitys.dt_oauth_app>((s, a) => new JoinQueryInfos(JoinType.Inner, a.id == s.oauth_id))
                    .Where((s, a) => a.is_lock == 0)
                    .Where(whereExpression)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .Select((s, a) => new Entitys.dt_site_oauth
                    {
                        id = s.id,
                        site_id = s.site_id,
                        oauth_id = s.oauth_id,
                        title = s.title,
                        app_id = s.app_id,
                        app_key = s.app_key,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        ptitle = a.title,
                        img_url = a.img_url,
                        remark = a.remark,
                        api_path = a.api_path,
                        is_lock = a.is_lock
                    })
                    .Take(top)
                    .ToList();

            }
        }

        public List<Entitys.dt_site_oauth> GetList(int top, string strWhere)
        {
            if (top == 0)
            {
                return db.Queryable<Entitys.dt_site_oauth, Entitys.dt_oauth_app>((s, a) => new JoinQueryInfos(JoinType.Inner, a.id == s.oauth_id))
                    .Select((s, a) => new Entitys.dt_site_oauth
                    {
                        id = s.id,
                        site_id = s.site_id,
                        oauth_id = s.oauth_id,
                        title = s.title,
                        app_id = s.app_id,
                        app_key = s.app_key,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        ptitle = a.title,
                        img_url = a.img_url,
                        remark = a.remark,
                        api_path = a.api_path,
                        is_lock = a.is_lock
                    })
                    .Where(s => s.is_lock == 0)
                    .Where(strWhere)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .ToList();
            }
            else
            {
                return db.Queryable<Entitys.dt_site_oauth, Entitys.dt_oauth_app>((s, a) => new JoinQueryInfos(JoinType.Inner, a.id == s.oauth_id))
                    .Where((s, a) => a.is_lock == 0)
                    .Where(strWhere)
                    .OrderBy(s => s.sort_id, OrderByType.Asc)
                    .OrderBy(s => s.id, OrderByType.Desc)
                    .Select((s, a) => new Entitys.dt_site_oauth
                    {
                        id = s.id,
                        site_id = s.site_id,
                        oauth_id = s.oauth_id,
                        title = s.title,
                        app_id = s.app_id,
                        app_key = s.app_key,
                        sort_id = s.sort_id,
                        add_time = s.add_time,

                        ptitle = a.title,
                        img_url = a.img_url,
                        remark = a.remark,
                        api_path = a.api_path,
                        is_lock = a.is_lock
                    })
                    .Take(top)
                    .ToList();

            }
        }

        public List<Entitys.dt_site_oauth> GetList(int pageSize, int pageIndex, Expression<Func<Entitys.dt_site_oauth, bool>> whereExpression, out int recordCount)
        {
            recordCount = 0;
            //A.title as ptitle,A.img_url,A.remark,A.api_path,A.is_lock
            //.Select((s, a) => new { ptitle=a.title,img_url=a.img_url,remark=a.remark,api_path=a.api_path,is_lock=a.is_lock })
            return db.Queryable<Entitys.dt_site_oauth, Entitys.dt_oauth_app>((s, a) => new JoinQueryInfos(JoinType.Inner, a.id == s.oauth_id))

                .Where((s,a) => a.is_lock == 0)
                .Where(whereExpression)
                .OrderBy(s => s.sort_id, OrderByType.Asc)
                .OrderBy(s => s.id, OrderByType.Desc)
                .Select((s, a) => new Entitys.dt_site_oauth
                {
                    id = s.id,
                    site_id = s.site_id,
                    oauth_id = s.oauth_id,
                    title = s.title,
                    app_id = s.app_id,
                    app_key = s.app_key,
                    sort_id = s.sort_id,
                    add_time = s.add_time,

                    ptitle = a.title,
                    img_url = a.img_url,
                    remark = a.remark,
                    api_path = a.api_path,
                    is_lock = a.is_lock
                })
                .ToPageList(pageIndex, pageSize, ref recordCount);
        }

    }
}